package com.javafactroy.kunlong.common.utils;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    public static String[] WeekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};


    public static Date parseStringLong2Date(String sday) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        return formatter.parse(sday, pos);
    }

    public static Integer transDate2Ym(Date d) {
        if (d == null) {
            return -1;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return Integer.valueOf(sdf.format(d));
    }

    public static Date mergeDateCard2Datetime(Date d, String card) {
        if (card.isEmpty()) {
            return d;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        String sd = transDate2String(d);
        String time = sd + " " + card +":00";
        return parseStringLong2Date(time);
    }

    //HH:mm
    public static String transDate2Card(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(d);
    }


    public static String transDatetime2LongString(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);
    }


    public static String transDate2String(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }


    public static int dayOfWeek(Date date) throws ParseException {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;

        if (w < 0) {
            w = 0;
        }
        return w;
    }

    //转星期
    public static String dateToWeek(Date date) throws ParseException {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return WeekDays[w];
    }


    public static String dateToWeek(String datetime) throws ParseException {

        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Date date;

        date = f.parse(datetime);
        cal.setTime(date);

        //一周的第几天
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return WeekDays[w];
    }



//    @InitBinder
//    public void initBinder(WebDataBinder binder, WebRequest request) {
//        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
//    }
    public static String transDatetime(Date d) {
        if(d==null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(d);
    }


    public static String transDate(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }

    public static String transDateSimple(Date d) {
        if (d == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(d);
    }

    public static Date getFirstDayOfMonth (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set( Calendar.DAY_OF_MONTH,1);
        return calendar.getTime();

    }

    public static Date getLastDayOfMonth (Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set( Calendar.DAY_OF_MONTH,1);
        calendar.add( Calendar.MONTH,1);
        calendar.add( Calendar.DAY_OF_MONTH,-1);
        return calendar.getTime();
    }  //年月日时分

    //考勤专用

    public static Long getMin(String sdate, String edate) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date start = simpleDateFormat.parse(sdate);

        Date end = simpleDateFormat.parse(edate);

        long between = (end.getTime() - start.getTime()) / 1000;

        long min = between / 60;

        return min;

    }
//    protected  Date parseStringLong2Date(String sday){
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        ParsePosition pos = new ParsePosition(0);
//        return  formatter.parse(sday, pos);
//    }

}
